#include "Zone.h"

using namespace std;

Zone::Zone(){}

//********************************************************************************************
// zone constructor
//
//********************************************************************************************
Zone::Zone(float x, float y, float size)
{
	this->x = x;
	this->y = y;
	this->size = size;
}

//********************************************************************************************
//********************************************************************************************
//                                   GETTERS and SETTERS
//
//********************************************************************************************
//********************************************************************************************
float Zone::getX()
{
	return this->x;
}

float Zone::getY()
{
	return this->y;
}

float Zone::getSize()
{
	return this->size;
}

int Zone::getNbIndices()
{
	return this->nbIndices;
}

int Zone::getNbVertices()
{
	return this->nbVertices;
}

short* Zone::getIndices()
{
	return this->indices;
}

CUSTOMVERTEX* Zone::getVertices()
{
	return this->vertices;
}

void Zone::setNbIndices(int size)
{
	this->nbIndices = size;
}

void Zone::setNbVertices(int size)
{
	this->nbVertices = size;
}

void Zone::setIndices(short *indices)
{
	this->indices = indices;
}

void Zone::setVertices(CUSTOMVERTEX *vertices)
{
	this->vertices = vertices;
}

//********************************************************************************************
//********************************************************************************************
//                                 /GETTERS and SETTERS
//
//********************************************************************************************
//********************************************************************************************




//********************************************************************************************
// zone to string on cout
//
//********************************************************************************************
void Zone::toString()
{
	cout << "********** Zone **********" << endl;

	cout << "Size : " << size << endl;
	cout << "X : " << x << endl;
	cout << "Y : " << y << endl;

	cout << endl << "Vertices : " << nbVertices << endl;

	for(unsigned int i = 0 ; i < nbVertices ; i++)
	{
		cout << "{" << this->vertices[i].X << ", ";
		cout << this->vertices[i].Y << ", " ;
		cout << this->vertices[i].Z << ", " ;
		cout << "(" << this->vertices[i].XN << "," ;
		cout << this->vertices[i].YN << "," ;
		cout << this->vertices[i].ZN << "), " ;
		cout << this->vertices[i].COLOR << "}" << endl;
	}

	cout << endl << "Indices : " << nbIndices << endl;

	for(unsigned int i = 0 ; i < nbIndices ; i++)
	{
		cout << this->indices[i] << ", ";

		if (i%3 == 2)
		{
			cout << endl;
		}
	}

	cout << "********** **** **********" << endl;

}

//********************************************************************************************
// initialize the buffers
//
//********************************************************************************************
void Zone::initialize()
{
	v_buffer = NULL;
	i_buffer = NULL;

    d3ddev->CreateVertexBuffer(nbVertices*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &v_buffer,
                               NULL);

	VOID* pVoid;


    v_buffer->Lock(0, 0, (void**)&pVoid, 0);
	memcpy(pVoid, vertices, nbVertices*sizeof(CUSTOMVERTEX));
    v_buffer->Unlock();

	d3ddev->CreateIndexBuffer(nbIndices*sizeof(short),
                              0,
                              D3DFMT_INDEX16,
                              D3DPOOL_MANAGED,
                              &i_buffer,
                              NULL);
	 
    i_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, indices, nbIndices*sizeof(short));
    i_buffer->Unlock(); 
}

//********************************************************************************************
// render the zone
//
//********************************************************************************************
void Zone::render(){

	// set the stream sources
    d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
    d3ddev->SetIndices(i_buffer);

	// and draw the triangles
    d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, nbVertices, 0, nbIndices/3);
}

//********************************************************************************************
// release the buffers
//
//********************************************************************************************
void Zone::release()
{
	v_buffer->Release();
    i_buffer->Release();
}
